package com.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.web.entity.AdminUser;
import com.web.entity.SysUser;
import com.web.service.AdminUserService;
import com.web.service.SysUserService;

@Controller
@RequestMapping("admin")
public class AdminController {

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private AdminUserService adminUserService;

	Logger logger = LogManager.getLogger();

	@RequestMapping("index")
	public String index() {
		logger.error("后台被访问");
		return "manage/index";
	}

	@RequestMapping("getAlluser")
	public String getAlluser() {
		return "manage/SYSUser";
	}

	@RequestMapping("tapPersonal")
	public String tapPersonal() {
		return "manage/AdminPersonal";
	}

	@RequestMapping("main")
	public String main() {
		return "manage/main";
	}

	@RequestMapping("login")
	public String login() {
		return "manage/login";
	}

	@RequestMapping("tapFlow")
	public String tapFlow() {

		return "manage/FlowBlog";
	}

	@RequestMapping("tapPaging")
	public String tapPaging() {

		return "manage/pagingBlog";
	}

	@ResponseBody
	@RequestMapping("/Userlist")
	private PageInfo<SysUser> list(SysUser sysUser, Integer pageNum, Integer pageSize) {

		return sysUserService.getListByPage(sysUser, pageNum, pageSize);
	}
	/*
	 * @ResponseBody
	 * 
	 * @RequestMapping("cpu") public double Cpu(Model model) { return
	 * getCpuUse.getProcessCpu()+Math.random() * 40; }
	 */

	@ResponseBody
	@RequestMapping("doLogin")
	public Map<String, Object> doLogin(String username, String password, String keycode, HttpServletRequest request,
			Model model) {
		System.out.println(username + " " + password);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String sessionCode = (String) request.getSession().getAttribute("captcha");
		// System.out.println(sessionCode+"@@@@@@@@@@@@@@@@@@");
		// System.out.println(keycode+"@@@@@@@@@@@@@@@@@@@@@@");
		if (keycode == null || !sessionCode.equals(keycode.trim().toLowerCase())) {
			resultMap.put("result", -1);
		} else {
			// 进入登录阶段完成后,从session中移除验证码
			request.getSession().setAttribute("Admin", username);
			request.getSession().removeAttribute("captcha");
			resultMap = adminUserService.doAdminUserLogin(username, password);
		}
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("show")
	public Map<String, Object> show(String username, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		username = (String) request.getSession().getAttribute("Admin");
		List<AdminUser> list = adminUserService.secletAdminUser(username);
		// System.out.println(username + "@@@@#####################@@@");
		// System.out.println(JSONObject.toJSON(adminUserService.secletAdminUser(username)));
		System.out.println("++++++@@@@ " + list.get(0).getName());
		map.put("id", list.get(0).getId());
		map.put("username", list.get(0).getUsername());
		map.put("password", list.get(0).getPassword());
		map.put("path", list.get(0).getPath());
		map.put("email", list.get(0).getEmail());
		map.put("sign", list.get(0).getSign());
		map.put("name", list.get(0).getName());
		// System.out.println(map);
		return map;
	}

	@ResponseBody
	@RequestMapping("updateAdminInfo")
	public Map<String, Object> updateAdminInfo(AdminUser adminUser) {
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println(adminUser.getUsername() + " !!!!#############");
		map = adminUserService.updateAdminInfo(adminUser);
		return map;
	}

	@ResponseBody
	@RequestMapping("ResetUser")
	public Map<String, Object> ResetUser(SysUser sysUser) {
		Map<String, Object> map = new HashMap<String, Object>();
		map = sysUserService.ResetUserPassword(sysUser);
		return map;
	}

}
